/******************************************************************************
**                                                                           **
**                 Proprietary and Confidential Information                  **
**                                                                           **
**                 Copyright (c) Conexant Systems, Inc. Unpublished                  **
**                                                                           **
** This source code and the algorithms implemented therein constitute        **
** confidential information and may compromise trade secrets of Conexant Systems,    **
** Inc. or its associates, and any unauthorized use thereof is prohibited.   **
**                                                                           **
******************************************************************************/

#ifndef _dc_jpeg_ctx_api_h_
#define _dc_jpeg_ctx_api_h_

/******************************************************************************
*  FILE FORMAT:
*      1. INCLUDES 
*      2. ENUMERATIONS 
*      3. DEFINES
*      4. STRUCTURES
*      5. GLOBALS
*      6. PRIVATE FUNCTION PROTOTYPES
*      7. FUNCTIONS
******************************************************************************/

/******************************************************************************
*  1. INCLUDES
******************************************************************************/
#ifdef LINUX
#include <stdio.h>
#endif
#include <string.h>
#include "dc_type.h"
#include "dc_return_codes.h"
#ifdef LINUX
#include "ospl.h"
#else
#include "tx_api.h"
#endif
#include "dc_log_api.h"
#include "dc_int_api.h"
#include "dc_container_api.h"
#include "dc_imgbuf_api.h"
#include "dc_dmbimg_api.h"
#include "dc_conip_api.h"
#include "dc_conip_extended_api.h"
#include "dc_conjp_api.h"
#include "dc_conro_api.h"
#include "dc_conjh_api.h"

#include "dc_jpeg_term_api.h"

/******************************************************************************
*  2. ENUMERATIONS
******************************************************************************/
typedef enum {
  FORMAT_XFORM_NONE = 0,
  FORMAT_XFORM_565,   // to PIXEL-INTERLEAVED 565
  FORMAT_XFORM_888    // to PIXEL-INTERLEAVED 888
} FORMAT_XFORM_t;

typedef enum {
  DEBUG_STATE_JH =0,
  DEBUG_STATE_JP,
  DEBUG_STATE_RO,
  DEBUG_STATE_IP,
  DEBUG_STATE_SUB,
  DEBUG_STATE_LAST
} DEBUG_STATE_t;
/******************************************************************************
*  3. DEFINES
******************************************************************************/

#if LINUX_PERF_DEBUG
#define LINUX_PERF_EDQ
#define JPEG_EDQ_SIZE   32  //CM_MAX_EDQ_SIZE
#endif

#define MAX_CONTEXTS  (MAX_COMPS_IN_SCAN + 1)

#if 0
#define MAX_INPUT_ADDRESS_STATE  2
#define MAX_OUTPUT_ADDRESS_STATE  2
#else
#define MAX_DQZ_RUN_BUFFER_LINES  2    // must be even
#define MAX_INPUT_ADDRESS_STATE   (MAX_DQZ_RUN_BUFFER_LINES/2 + 1)  // 4 block states * half of runs
#define MAX_OUTPUT_ADDRESS_STATE  (MAX_DQZ_RUN_BUFFER_LINES/2 + 1)
#endif

#if (MAX_INPUT_ADDRESS_STATE >= MAX_OUTPUT_ADDRESS_STATE)
#define MAX_ADDRESS_STATE  MAX_INPUT_ADDRESS_STATE
#else
#define MAX_ADDRESS_STATE  MAX_OUTPUT_ADDRESS_STATE
#endif

#define TOTAL_RO_JP_ROWS   2
#define TOTAL_JP_JH_ROWS   2

#define MIN_UNGET    4

#ifdef DC_WORKAROUND_ITS_A0
#define TEMP_INFO_SIZE    32
#define TEMP_INFO_SIZE2   (TEMP_INFO_SIZE<<1)
#endif
/******************************************************************************
*  4. STRUCTURES
******************************************************************************/
typedef struct {
  UINT32 address;
  UINT32 length;
} JPEG__MEM_t;

typedef struct {
  void *arg;
  UINT32 value;
} JPEG_FLAG_INFO_t;

typedef struct {
  JPEG__MEM_t temp;

  JPEG__MEM_t jp_jh[TOTAL_JP_JH_ROWS];
  JPEG__MEM_t sub_jp_jh[TOTAL_JP_JH_ROWS];  // decode 4x4 transform
  JPEG__MEM_t ro_jp[MAX_COMPS_IN_SCAN+1][TOTAL_RO_JP_ROWS];

  JPEG__MEM_t dqz_jp_jh[MAX_DQZ_RUN_BUFFER_LINES];
  JPEG__MEM_t dqz_ro_jp[MAX_DQZ_RUN_BUFFER_LINES];

  JPEG__MEM_t ip_io;
  JPEG__MEM_t ip_erd;

  JPEG__MEM_t xform;

} JPEG_BUFFERS_t;

typedef struct {
  DC_DMBIMG     *ptr_dmbimg;
  DC_DMBIMG_PRODUCER *ptr_producer;
  DC_DMBIMG_CONSUMER *ptr_consumer;

  UINT32 image_width;
  UINT32 image_height;

  DC_CONJH_RAM_TABLE huffman_ram_table[2];

  DC_CONJH_DATA context;  
  UINT32 w_tick_count;
  UINT32 r_tick_count;

  UINT32 n_used_bytes;
  UINT32 n_previous_bytes;
  UINT32 n_queue_loaded;

  UINT32 state;

  UINT32 rw_index;
  UINT32 n_in_segments;   // for encode
  UINT32 n_out_segments;  // for decode
  UINT32 n_segments;

  UINT32 sub_w_tick_count;              // for sub thread
  UINT32 sub_r_tick_count;              // for sub thread
  UINT32 sub_rw_index;                  // for sub thread
  UINT32 sub_n_mcus[TOTAL_JP_JH_ROWS];  // for sub thread

  UINT32 n_mcus;
  UINT32 n_mcus_interval_to_process;
  UINT32 n_mcus_to_process;
  UINT32 n_processed_mcus;

  UINT32 n_left_mcus_to_skip;
  UINT32 n_valid_mcus_to_process;
  UINT32 skip_mcus_flag;

  UINT32 n_mcu_lines_processed;
  UINT32 n_major_crop_mcu_lines;      // front crop
  UINT32 n_major_crop_mcu_last_lines; // back crop

  UINT32 mcu_address;
  JPEG__MEM_t address_state[MAX_ADDRESS_STATE];
  UINT32 address_state_index;

  JPEG_COMPRESS_INFO   compress_info;
  JPEG_COMPONENT_INFO  component_info[MAX_COMPS_IN_SCAN];
  DC_CONJH_INPUT_TABLE huff_input_table[NUM_HUFF_TBLS];
  DC_CONJH_MARKER_INFO marker_info;
  UINT8 quant_table[NUM_QUANT_TBLS][DCTSIZE2];
  UINT32 quant_precision[NUM_QUANT_TBLS];
  UINT8 quant_table_no[NUM_QUANT_TBLS];

  UINT32 n_worst_bytes_per_mcu;
//  UINT32 mem_data_wait;

  UINT32 error_value;
  void   *ptr_parent_context;
  
  UINT32 mi_write_req;
  UINT32 mi_write_available_cnt;
  UINT32 mi_write_address;
  UINT8  *p_cpu_write;

  UINT32 insert_dnl;
  UINT32 restart_interval_lines;

  DC_CONJH_DECODE_MODES decode_mode;

  UINT32 read_file_offset;       // the count read of file from container
  UINT32 consumed_file_offset;   // the count consumed
  UINT32 freed_file_offset;      // the count freed
  UINT32 end_of_file;

  //UINT8 (*byte_get_func) (void *);
  UINT32 n_bytes_in_buffer;
  UINT32 n_back_data;
  UINT8 *ptr_next_in_byte;
  UINT8 back_data[MIN_UNGET];
  UINT32 markerTypes;
  UINT32 coarseEstimatedBPM;
  UINT32 fineEstimatedBPM;

  UINT32 free_input;
  UINT32 completed;

  // at jh level, to pass the jpegpipe pointer to caller
  void *ptr_jpegpipe_context;
  UINT32 iter;

#ifdef LINUX_PERF_EDQ
  CM_EDQ_Table_t edq;
  UINT32 edq_q_length1;  // to avoid queue_status_get calls
  UINT32 edq_q_length2;
#endif

#ifdef DC_WORKAROUND_ITS_A0
  UINT32 temp_info_wptr_start;
  UINT32 temp_info_wptr_current;
  UINT32 temp_info_rptr_hw;
  UINT32 temp_info_rptr_sw;
  UINT32 temp_info_state;
  UINT32 temp_info_inbytes;
  UINT32 temp_info_inbytesnext;
  struct {
    UINT32 address;
    UINT32 length;
    UINT32 read_file_offset;
  } temp_info_save[TEMP_INFO_SIZE];
#endif

} DC_JPEG_JH_WORK_t;

typedef struct {
  DC_CONJP_DATA context[MAX_CONTEXTS];
  UINT32 context_count;
  UINT32 tick_count;
  UINT32 w_tick_count;
  UINT32 r_tick_count;

  UINT32 state;
  UINT32 rw_index;
  UINT32 n_in_segments;
  UINT32 n_out_segments;
  UINT32 n_segments;
  //UINT32 n_prev_segments;

  UINT32 completed;
} DC_JPEG_JP_WORK_t;


typedef struct {
#ifdef LINUX
  OSPL_SEMAPHORE  flags_semaphore;
#else
  TX_SEMAPHORE  flags_semaphore;
#endif  
  UINT32    flags;

  DC_IMGBUF     *ptr_imgbuf;
  DC_IMGBUF_PRODUCER *ptr_producer;
  DC_IMGBUF_CONSUMER *ptr_consumer;

  DC_CONRO_DATA context[MAX_CONTEXTS];

  JPEG_FLAG_INFO_t flag_info;   // in or out

  UINT32 context_count;
  UINT32 tick_count;
  UINT32 w_tick_count;
  UINT32 r_tick_count;

  UINT32 imgbuf_base;
  UINT32 imgbuf_stride;
  UINT32 imgbuf_ysize;
  UINT32 imgbuf_width;
  UINT32 imgbuf_height;
  //UINT32 imgbuf_chans;

  UINT32 state;

  UINT32 target_height;
  INT32 produced_lines;
  UINT32 issued_lines;
  UINT32 consumed_lines;

  UINT32 buf_alert_on;
  UINT32 buf_alert_flag;
  UINT32 buf_alert_lines;

  UINT32 rw_index;
  UINT32 n_in_segments;
  UINT32 n_out_segments;
  UINT32 n_segments;
  UINT32 n_prev_segments;

  UINT32 free_input;

  UINT32 data_move;   // for crop
  UINT32 this_lines;
  UINT32 iter;

  UINT32 completed;
} DC_JPEG_RO_WORK_t;

typedef struct {
#ifdef LINUX	
  OSPL_SEMAPHORE  flags_semaphore;
#else
  TX_SEMAPHORE  flags_semaphore;
#endif  
  UINT32    flags;

  DC_IMGBUF     *ptr_imgbuf;
  DC_IMGBUF     *ptr_client_imgbuf;

  DC_IMGBUF_CONSUMER *ptr_consumer;
  DC_IMGBUF_PRODUCER *ptr_producer;

  DC_CONIP_DATA context;

  JPEG_FLAG_INFO_t flag_info[2];  // in and out

  UINT32 state;
  //UINT32 toggle;
  UINT32 this_lines;

  UINT32 target_height;
  UINT32 input_height;

  UINT32 min_input_lines;
  UINT32 min_output_lines;
  UINT32 issued_lines;
  UINT32 q_input_lines;
  UINT32 q_output_lines;
  UINT32 produced_lines;
  UINT32 input_issued_lines;
  UINT32 input_consumed_lines;

  UINT32 cumulative_pad_lines;
  //UINT32 input_image_clear_lines;
  //UINT32 input_image_short_lines;
  UINT32 buf_alert_on;
  UINT32 buf_alert_flag;
  UINT32 buf_alert_lines;

  UINT32 free_input;

  //DC_CONIP_CSC_TABLE_DATA default_csc_table_data;
  DC_CONIP_CSC_TABLE_CONFIG csc_cfg;
  //UINT32 default_csc_mem[9*9*9*4/4];

} DC_JPEG_IP_WORK_t;

typedef struct {
  UINT32 state;

  UINT32 flags;
  UINT32 produced_lines;
  UINT32 target_lines;
  UINT32 this_lines;

  DC_IMGBUF     *ptr_imgbuf;
  DC_IMGBUF     *ptr_client_imgbuf;

  DC_IMGBUF_CONSUMER *ptr_consumer;
  DC_IMGBUF_PRODUCER *ptr_producer;

  JPEG_FLAG_INFO_t flag_info[2];  // in and out

  UINT32 target_width;
  UINT32 src_base;
  UINT32 src_stride;
  UINT32 src_ysize;
  UINT32 src_yvalue;

  UINT32 dst_base;
  UINT32 dst_stride;
  UINT32 dst_ysize;
  UINT32 dst_yvalue;

  UINT32 stop;

} DC_JPEG_SUB_XFORM_WORK_t;

typedef struct {
  UINT32 prev2;
  UINT32 prev1;
  UINT32 cur;
} DC_JPEG_DEBUG_STATE_t;

typedef struct {

  DC_JPEGPIPE_ERROR_t error_type;

  // pros.and cons to client containers for decode
  DC_IMGBUF_PRODUCER imgbuf_producer;
  DC_IMGBUF_CONSUMER imgbuf_consumer;

  DC_DMBIMG_CONSUMER dmbimg_consumer;
  DC_DMBIMG_PRODUCER dmbimg_producer;

  DC_IMGBUF          ip_imgbuf;
  DC_IMGBUF_PRODUCER ip_imgbuf_producer;
  DC_IMGBUF_CONSUMER ip_imgbuf_consumer;

  UINT32 ip_bypass;
  DC_JPEG_IP_WORK_t ip_work;
  DC_JPEG_RO_WORK_t ro_work;
  DC_JPEG_JP_WORK_t jp_work;
  UINT32            jp_image_format;
  DC_JPEG_JH_WORK_t jh_work;

  DC_IMGBUF          sub_xform_imgbuf;
  DC_IMGBUF_PRODUCER sub_xform_imgbuf_producer;

  UINT32 ip_run_line_mode;

  FORMAT_XFORM_t output_xform_mode;
  FORMAT_XFORM_t fw_xform_flag;

  DC_JPEG_SUB_XFORM_WORK_t sub_xform_work;

  JPEG_BUFFERS_t static_buffers;

  UINT32 line_units;
  UINT32 width_units;

  UINT32 shifted_line_units;
  UINT32 shifted_width_units;

  UINT32 n_produced_lines;

  UINT32 n_mcu_lines;
  UINT32 n_mcus_mem_unit;

  UINT32 n_mcus_per_line;
  UINT32 n_segments_per_mcu_line;

  UINT32 n_completed_b2l_mcus;

  UINT32 attribute_status;

  UINT32 crop_width;
  UINT32 crop_height;
  UINT32 minor_crop_y_lines;
  UINT32 major_crop_y_lines;
  UINT32 minor_crop_x_pixels;
  UINT32 major_crop_x_pixels;

  DC_JPEG_DEBUG_STATE_t debug_state[DEBUG_STATE_LAST];

  UINT32 n_dqz_run_buffer_lines;
  UINT32 n_dqz_run_unit;
  UINT32 couple_enabled;
  UINT32 dqz_enabled;
} DC_JPEG_CTX_t;


#endif
